﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Autocentro.Entities;
using Autocentro.BussinesLogic;


namespace Autocentro.UI
{
    public partial class FrmMarca : Form
    {
        [Dependency]
        public IMarcaBL MarcaBL { get; set; }

        public FrmMarca()
        {
            InitializeComponent();
            this.BuildUp();
            mARCABindingSource.DataSource = MarcaBL.GetAllFromMarcas();
        }

        private void FrmMarca_Load(object sender, EventArgs e)
        {
            
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargarData();
        }

        private void CargarData()
        {
            var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
            mARCABindingSource.DataSource = MarcaBL.GetAFromMarcasByCriterio(criterio);
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargarData();
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var nMarca = new FrmMttoMarca();
            nMarca.AddMarca();
            nMarca.ShowDialog();
            
            mARCABindingSource.DataSource = MarcaBL.GetAllFromMarcas();
            
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var _marca = mARCABindingSource.Current as MARCA;

            if (_marca!=null)
            {
                var edit = new FrmMttoMarca();
                edit.marca = _marca;
                edit.UpdateMarca();
                edit.ShowDialog();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar una Marca");
            }
            mARCABindingSource.DataSource = MarcaBL.GetAllFromMarcas();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var marca = mARCABindingSource.Current as MARCA;

            if (marca != null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea eliminar la marca: \n {0}?", marca.Descripcion),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    MarcaBL.RemoveMarca(marca.Id);
                    CargarData();
                }
            }
            else
            {
                MessageBox.Show("Debe de seleccionar una Marca");
            }
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        
    }
}
